package com.gill.controller;

import com.gill.exception.CommonServiceException;
import com.gill.service.UserService;
import com.gill.util.JwtTokenUtil;
import com.gill.vo.BaseResponseVO;
import com.gill.vo.LoginReqVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@RestController
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/test")
    public void test(){
        System.out.println("test.................");
    }

    @PostMapping("/login")
    public BaseResponseVO login(@RequestBody LoginReqVO reqVO) throws CommonServiceException {

        // 数据验证
        reqVO.checkParam();

        // 用户密码验证
        String uuid = userService.checkUserLogin(reqVO.getUsername(), reqVO.getPassword());

        // 生成token
        String randomKey = JwtTokenUtil.getRandomKey();
        String token = JwtTokenUtil.generateToken(uuid, randomKey);

        //设置返回数据（参考接口文档）
        HashMap<String,String> result = new HashMap<>();
        result.put("randomKey",randomKey);
        result.put("token",token);

        return BaseResponseVO.success(result);
    }
}
